/*
 * Created on 09/16/2011 - 01:22 PM
 */
package sembr.geometry.shapes;
import importadorDXF.DxfVERTEX;

/**
 *
 * @author Adriano
 */
public class PolyfaceCollector extends PolylineCollector
{    
    @Override
    public int getNrOfVertexAt(int index)
    {
        return getCurrentElement(index).getNrCtrlU();        
    }    
    
    @Override
    public double[][] getVerticesCoordenatesAt(int index)
    {
        // number of vertices of polyface
        int currentNrOfVertex = this.getNrOfVertexAt(index);
        
        // coordenates in 3D
        double[][] points = new double[currentNrOfVertex][3];
                
        // loop over all vertices
        // points are set with vertices that contains coordenates set
        for (int i = 0; i < currentNrOfVertex; i++)
        {
            // current vertex
            DxfVERTEX vertex = (DxfVERTEX)(getCurrentElement(index)).getVertices().getEntity(i);
                       
            // set points with current vertex´s coordenates
            points[i][0] = vertex.getPosition().x;
            points[i][1] = vertex.getPosition().y;
            points[i][2] = vertex.getPosition().z;                   
        }
    
        return points;
    }  
    
    public int getNrOfFacesAt(int index)
    {
        return getCurrentElement(index).getNrCtrlV();
    }
    
    public int[][] getFacesIndexesAt(int index)
    {
        // polyface array vertices are set first
        // the remaining vertices in the array are faces
        // so the index inside the loop will be offset until faces in the array
        // number of vertices
        int nrOfVertices = this.getNrOfVertexAt(index);
        // number of vertices that are faces
        int nrOfFaces = getNrOfFacesAt(index);
        
        // face has four indexes each
        int[][] indexes = new int[nrOfFaces][4];
                
        // initial value for the face index
        // face index begins after vertices
        int i;
        // index to correctly bound the indexes array
        int j = 0;
        
        // face index will reach the end of the array
        int totalNrOfVertices = nrOfVertices + nrOfFaces;
                
        for (i = nrOfVertices; i < totalNrOfVertices; i++)
        {
            DxfVERTEX vertex = (DxfVERTEX)(getCurrentElement(index)).getVertices().getEntity(i);
            
            j = i - nrOfVertices;        
            indexes[j] = vertex.getReferences();               
        }           
        
        return indexes;
    }    
}
